package org.bedracket.pgeg.network;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.PacketDistributor;
import org.bedracket.pgeg.init.ModCapabilities;

import java.util.function.Supplier;

public class ManaChangeMessage {

    public ManaChangeMessage(FriendlyByteBuf buf) {

    }

    public static void buffer(ManaChangeMessage message, FriendlyByteBuf buf) {

    }

    public static void handler(ManaChangeMessage message, Supplier<NetworkEvent.Context> context) {
        ServerPlayer player = context.get().getSender();
        if (player != null) {
            player.getCapability(ModCapabilities.PLAYER_MANA_LEVEL).ifPresent(
                    capability -> {
                        SimpleNetworkHandler.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(()
                                -> player),
                                new PlayerManaMessage(capability.getManaLevel(),
                                        capability.getMaxManaLevel()));
                    }
            );
        }
        context.get().setPacketHandled(true);
    }
}
